import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('../views/login/Login')
const Home = () => import('../views/home/Home')
const Welcome = () => import('../views/home/Welcome')
const Users = () => import('../views/home/user/Users')
const Rights = () => import('../views/home/power/Rights')
const Roles = () => import('../views/home/power/Roles')
const Cate = () => import('../views/home/goods/Cate')
const Params = () => import('../views/home/goods/Params')
const GoodsList = () => import('../views/home/goods/List')
const AddGoods = () => import('../views/home/goods/AddGoods')
const Order = () => import('../views/home/order/Order')
const Report = () => import('../views/home/report/Report')

Vue.use(VueRouter)

const routes = [{
    path: "/",
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    children: [{
        path: '/',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: GoodsList
      },
      {
        path: '/goods/add',
        component: AddGoods
      },
      {
        path: '/orders',
        component: Order
      }, {
        path: '/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫 控制访问权限
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next();
  const token = window.sessionStorage.getItem('token');
  if (!token) return next('/login');
  next()
})

export default router